﻿using System;
using System.Linq.Expressions;
using System.Reflection;
using Loki.Utils;

namespace Luna.Services.Test
{
    public class PropertyShifter<TType, TProperty> : LokiObject, IPropertyShifter<TType>
    {
        private PropertyInfo _Info;

        private Func<TProperty> _ValueGetter;

        public PropertyShifter(Expression<Func<TType, TProperty>> P_Property, Func<TProperty> P_ValueGetter)
        {
            _Info = ExpressionHelper.GetProperty(P_Property);
            _ValueGetter = P_ValueGetter;
        }

        public void Shift(TType P_Target)
        {
            object L_OldValue = _Info.GetValue(P_Target);
            object L_NewValue = _ValueGetter();

            while (object.Equals(L_OldValue, L_NewValue))
            {
                L_NewValue = _ValueGetter();
            }

            _Info.SetValue(P_Target, L_NewValue);
        }

        public void SameValue(TType P_Target)
        {
            object L_OldValue = _Info.GetValue(P_Target);
            _Info.SetValue(P_Target, L_OldValue);
        }
    }
}